package com.tengju.support.infrastructure.repository.message.convert;

import com.tengju.support.domain.model.message.*;
import com.tengju.support.domain.shared.JsonUtil;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.SupportMessageContentDO;
import org.apache.commons.lang3.StringUtils;


public class MessageContentConvert {

    public static MessageTask convertToModel(SupportMessageContentDO messageContentDO) {
        if (messageContentDO == null) {
            return null;
        }

        MessageUserOrientation messageUserOrientation = null;
        if (!StringUtils.isEmpty(messageContentDO.getUserOrientation())) {
            messageUserOrientation =
                    JsonUtil.toObject(messageContentDO.getUserOrientation(), MessageUserOrientation.class);
        }
        MessageType messageType = MessageType.fromVal(messageContentDO.getType());
        MessageChannel messageChannel = MessageChannel.parseByMessageType(messageType);

        return new MessageTask(
                new TaskId(messageContentDO.getId()),
                messageContentDO.getTitle(),
                messageChannel,
                messageType,
                MessageContentType.fromVal(messageContentDO.getContentType()),
                messageContentDO.getImgUrl(),
                messageContentDO.getContent(),
                messageContentDO.getJumpUrl(),
                messageUserOrientation,
                messageContentDO.getPush(),
                SendStatus.fromVal(messageContentDO.getSendStatus()),
                messageContentDO.getCreateBy(),
                messageContentDO.getUpdateBy(),
                messageContentDO.getSendTime(),
                messageContentDO.getCreateTime(),
                messageContentDO.getUpdateTime()
        );
    }

    public static SupportMessageContentDO convertToDO(MessageTask messageTask) {
        if (messageTask == null) {
            return null;
        }
        return new SupportMessageContentDO(
                messageTask.getTaskId() == null ? null : messageTask.getTaskId().getValue(),
                messageTask.getTitle(),
                messageTask.getMessageChannel().getVal(),
                messageTask.getMessageType().getVal(),
                messageTask.getContentType().getVal(),
                messageTask.getImgUrl(),
                messageTask.getContent(),
                messageTask.getJumpUrl(),
                JsonUtil.toJSONString(messageTask.getMessageUserOrientation()),
                messageTask.getPush(),
                messageTask.getSendStatus().getVal(),
                messageTask.getCreateBy(),
                messageTask.getUpdateBy(),
                messageTask.getSendTime(),
                null,
                null
        );
    }
}
